import { ChangeDetectorRef, Component, OnInit } from "@angular/core";
import { _HttpClient } from "@delon/theme";
import { NzMessageService } from "ng-zorro-antd/message";
import { UserService } from "../user-list/user.service";
import { NzUploadChangeParam } from "ng-zorro-antd/upload";

@Component({
  selector: "app-user-setting",
  templateUrl: "./user-setting.component.html",
  styles: []
})
export class UserSettingComponent implements OnInit {


  constructor(private http: _HttpClient,
              private cdr: ChangeDetectorRef,
              private msg: NzMessageService,
              private userSrv: UserService) {
  }

  userLoading = true;
  user: any = {};

  ngOnInit(): void {

    this.getCurrentUser();
  }

  handleChange(info: NzUploadChangeParam): void {
    if (info.file.status !== "uploading") {

    }
    if (info.file.status === "done") {
      if (info.file.response.code === 1) {
        this.user.avatarId = info.file.response.data.id;
        this.msg.success(`${info.file.name} file uploaded successfully`);
      }
    } else if (info.file.status === "error") {
      this.msg.error(`${info.file.name} file upload failed.`);
    }
  }

  // 获取当前用户
  getCurrentUser() {

    this.userLoading = true;

    this.userSrv.getCurrentUser().subscribe(res => {

      if (res.code === 1) {
        this.userLoading = false;
        this.user = res.data;
      }
    });
  }


  // 保存
  save(): boolean {
    this.userSrv.updateInfo(this.user).subscribe(res => {
      if(res.code === 1){
        this.msg.success("修改完成");
      }else{
        this.msg.success("修改完成");
      }

    });

    return false;
  }
}
